package br.com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.connection.ConnectionFactory;
import br.com.controller.NotaFaltaBimestralController;

/**
 * Servlet implementation class CadNotaFaltaServlet
 */
@WebServlet("/CadNotaFaltaServlet")
public class CadNotaFaltaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CadNotaFaltaServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	/*	PrintWriter out = response.getWriter();
		try{
			doInsert(request, response);
		}catch (Exception e){
			out.println("ocorreu algum erro");
		}		*/
		
		PrintWriter out = response.getWriter();

		
		String[] valores = request.getParameterValues("tamanho");
		
		for(int i=0; i < valores.length;i++){
			
			request.setCharacterEncoding("UTF-8");        
			response.setCharacterEncoding("UTF-8");
			int matr = Integer.parseInt(request.getParameter("matr"+i));
			int bim = Integer.parseInt(request.getParameter("bimestre"));
			int auladd = Integer.parseInt(request.getParameter("auladadas"));
			int disc = Integer.parseInt(request.getParameter("disciplina"));
			double nota = Double.parseDouble(request.getParameter("nota"+i));
			int falta = Integer.parseInt(request.getParameter("falta"+i));
			int aluno = Integer.parseInt(request.getParameter("aluno"+i));

			Connection conn = null;
			try {

				conn = new ConnectionFactory().getConnection();

				NotaFaltaBimestralController.inserir(conn, matr, bim, auladd, disc, nota, falta, aluno);
				conn.commit();

			} catch (Exception e) {
				e.printStackTrace();
				try {
					conn.rollback();
				} catch (Exception ex) {ex.printStackTrace();}
			} finally {
				try {
					conn.close();
				} catch (Exception ex) {}
				out.print("<script type=\"text/javascript\">alert(\"Cadastrado com sucesso.\");</script>");
				out.print("<meta http-equiv='refresh'>");
			}
		}	
		
	}

	protected void doInsert(HttpServletRequest request, HttpServletResponse response) throws Exception {

		
		
	}
}


